package com.szgr.baoyin.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.BankTranslateActivity;
import com.szgr.baoyin.activity.GoodsDetailActivity;
import com.szgr.baoyin.activity.GoodsJudgeActivity;
import com.szgr.baoyin.adapter.MyOrderListAdapter;
import com.szgr.baoyin.model.OrderInfo;
import com.szgr.baoyin.pay.AccessoryProtectPayActivity;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class OrderToLeaveFragment extends Fragment implements View.OnClickListener {
    private List<OrderInfo> data;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    listAdapter.setData(data);
                    rlNoRecord.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    break;
                case Constant.REQUEST_FAIL:
                    rlNoRecord.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.GONE);
                    break;
            }
        }
    };
    private MyOrderListAdapter listAdapter;
    private RelativeLayout rlNoRecord;
    private ListView listView;

    public OrderToLeaveFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_order_to_leave, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        data = new ArrayList<>();
        rlNoRecord = (RelativeLayout) view.findViewById(R.id.no_record);
        listView = (ListView) view.findViewById(R.id.order_leave_list_view);
        listAdapter = new MyOrderListAdapter(getActivity(), data,this);
        listView.setAdapter(listAdapter);

        getOrder();
    }

    private void getOrder() {
//        DialogUtils.showLoadingDialog(getActivity(), "正在拼命加载中...", false);
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_ORDER_LIST,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("-----order list----->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if(jsonObject.has("datas")){
                                JSONObject datas = jsonObject.getJSONObject("datas");
                                if(datas.has("order_group_list")){
                                    JSONArray order_group_list = datas.getJSONArray("order_group_list");
                                    Gson gson = new Gson();
                                    data = gson.fromJson(order_group_list.toString(),new TypeToken<List<OrderInfo>>(){}.getType());
                                    if(data.size() == 0){
                                        mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
                                    }else{
                                        mHandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
                                    }

                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mHandler.sendEmptyMessage(1008);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key", MyApplication.getmInstance().getKey());
                map.put("order_state","40");
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.order_number:
                int tag2 = (int) v.getTag();
                OrderInfo orderInfo2 = data.get(tag2);
                OrderInfo.MyOrderItem myOrderItem1 = orderInfo2.getOrder_list().get(0);
                Intent intent1 = new Intent(getActivity(), GoodsDetailActivity.class);
                intent1.putExtra("id",myOrderItem1.getOrder_id());
                startActivity(intent1);
                break;
            case R.id.order_goods_btn:
                int tag = (int) v.getTag();
                OrderInfo orderInfo = data.get(tag);
                List<OrderInfo.MyOrderItem> order_list = orderInfo.getOrder_list();
                String order_state = order_list.get(0).getOrder_state();
                switch (order_state){
                    case "40":
                        //TODO 评价
                        Intent intent = new Intent(getActivity(), GoodsJudgeActivity.class);
                        intent.putExtra("extendsgoods",orderInfo);
                        startActivity(intent);
                        break;
                }
                break;
        }
    }
}
